package hisp_interface;

import java.io.ByteArrayInputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Message {
	private final String defaultMessage = "<message></message>";
	//private final String defaultMessage = "<message><identity><id /><password /><salt /><xml/></identity><healthrecord><id/><private/><public/><acl /><doctor /><identifier/></healthrecord><key><id/><private/><public/></key></message>";
	private Document message = null;
	
	public Message() {
		SAXBuilder builder = new SAXBuilder();
		try {
			message = builder.build(new ByteArrayInputStream(defaultMessage.getBytes()));			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Message(String message) {
		SAXBuilder builder = new SAXBuilder();
		try {
			this.message = builder.build(new ByteArrayInputStream(message.getBytes()));			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	public String getMessage () {
		Element root = message.getRootElement();
		return root.getAttribute("context").getValue();
	}
		
	public Message setMessage (String type) {
		Element root = message.getRootElement();
		root.setAttribute("context", type);	
		return this;
	}
	
	public Message setIdentity (String id, String password, String salt, String xml)
	{
		Element root = message.getRootElement();
		Element identity = new Element("identity");
		Element old = root.getChild("identity");
		
		if (old == null) {
			if (id != null) identity.addContent(new Element("id").setText(id));
			if (password != null) identity.addContent(new Element("password").setText(password));
			if (salt != null) identity.addContent(new Element("salt").setText(salt));
			if (xml != null) identity.addContent(new Element("xml").setText(xml));
			root.addContent(identity);
		}
		else {
			updateElement(old, "id", id);
			updateElement(old, "password", password);
			updateElement(old, "salt", salt);
			updateElement(old, "xml", xml);
		}
		return this;
	}
	
	private void updateElement(Element e, String itemname, String content) {
		if (e.getChild(itemname) == null) e.addContent(new Element(itemname).setText(content));
		else if (content != null) e.getChild(itemname).setText(content);	
	}
	
	public String getIdentityAttribute(String index) {
		Element root = message.getRootElement();
		Element identity = (Element) root.getChild("identity");
		return identity.getChild(index).getValue();		
	}
			
	public Message setHealthRecord(String id,String privateRecord, String publicRecord, String acl, String doctor, String identifier) 
	{
		Element root = message.getRootElement();
		Element hr = new Element("healthrecord");
		Element old = root.getChild("healthrecord");
		
		if (old == null) {
			if (id != null) hr.addContent(new Element("id").setText(id));
			if (privateRecord != null) hr.addContent(new Element("private").setText(privateRecord));
			if (publicRecord != null) hr.addContent(new Element("public").setText(publicRecord));
			if (acl != null) hr.addContent(new Element("acl").setText(acl));
			if (doctor != null) hr.addContent(new Element("doctor").setText(doctor));
			if (identifier != null) hr.addContent(new Element("identifier").setText(identifier));
		}
		else {
			updateElement(old, "id", id);
			updateElement(old, "private", privateRecord);
			updateElement(old, "public", publicRecord);
			updateElement(old, "acl", acl);
			updateElement(old, "doctor", doctor);
			updateElement(old, "identifier", identifier);
			
		}
		root.addContent(hr);
		return this;
	}

	public String getHealthRecordAttribute(String index) {
		Element root = message.getRootElement();
		Element identity = (Element) root.getChild("healthrecord");
		return identity.getChild(index).getValue();		
	}
	
	public Message setKey (String id, String identifier, String privateKey, String publicKey)
	{
		Element root = message.getRootElement();
		Element key = new Element("key");
		Element old = root.getChild("key");
		if (old == null ) {
			if (id != null) key.addContent(new Element("id").setText(id));
			if (identifier != null) key.addContent(new Element("identifier").setText(identifier));
			if (privateKey != null) key.addContent(new Element("private").setText(privateKey));
			if (publicKey != null) key.addContent(new Element("public").setText(publicKey));
			root.addContent(key);
		}
		else {
			updateElement(old, "id", id);
			updateElement(old, "identifier", identifier);
			updateElement(old, "private", privateKey);
			updateElement(old, "public", publicKey);
		}
		return this;
	}
	
	public String getKeyAttribute(String index) {
		Element root = message.getRootElement();
		Element identity = (Element) root.getChild("key");
		return identity.getChild(index).getValue();		
	}
	
	public String toXml()
	{
		XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
		return outputter.outputString(message).replaceAll("[\n\r]", "");
	}
}
